﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
namespace P01
{
    /// <summary>
    /// 获得采购收料磁卡信息
    /// </summary>
    public class A01 : IPDAChild
    {
        public void Code(IPDA handler,CodeResult CR, PDACodeUser user, String parameter)
        {
            P01A01P P = (P01A01P)handler.SerialApi.Deserialize(typeof(P01A01P), parameter, CR.SerialType);

            P01A01R R = new P01A01R();
            try
            {
                SqlParameter[] sps = new SqlParameter[1];
                sps[0] = new SqlParameter("@Serial", SqlDbType.NVarChar, 50);
                sps[0].Value = P.CardSerial;

                String sql = "SELECT A.FNumber,A.FComboBox Target,A.FText2 BillId,B.FNumber AccountNo FROM KEW_ICCardEntry A LEFT JOIN KEW_DataBaseEntry B ON A.FBase=B.FID WHERE A.FText=@Serial OR A.FText4=@Serial";
                Recordset rs = handler.Data.ExeBase(sql, sps);

                if (rs.Enable)
                {
                    if (rs.Data.Tables.Count != 1 || rs.Data.Tables[0].Rows.Count != 1)
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = "没有检索到该磁卡的信息";
                    }
                    else
                    {
                        if (rs.Data.Tables[0].Rows[0]["Target"].ToString() != "1")
                        {
                            CR.Result = EnumResult.Error;
                            CR.Message = "不是采购磁卡";
                        }
                        else
                        {
                            String AccountNo = rs.Data.Tables[0].Rows[0]["AccountNo"].ToString();
                            String BillId = rs.Data.Tables[0].Rows[0]["BillId"].ToString();
                            sql = "SELECT A.FBillNo,A.FText CarNumber,A.FDecimal4 SrcQty,A.FDecimal17 RedQty,A.FComboBox4 RedStyle,A.FText10 RedReason,A.FDecimal InsQty,A.FTime1 InsTime,A.FComboBox State,A.FComboBox2 MeterType,A.FNOTE Note,A.FText9 Checker,A.FText13 Address,A.FComboBox3 UnloadStyle,A.FText4 SampNo,B.FName ItemName,C.FName SupplyName FROM KEW_PurchInfo A ";
                            sql += "LEFT JOIN KEW_PurchItemEntry B ON A.FBase=B.FID LEFT JOIN t_Supplier C ON A.FBase1=C.FItemID WHERE A.FID=" + BillId;
                            rs = handler.Data.ExeProcedure(AccountNo, sql, null);
                            if (rs.Enable)
                            {
                                if (rs.Data.Tables.Count != 1 || rs.Data.Tables[0].Rows.Count != 1)
                                {
                                    CR.Result = EnumResult.Error;
                                    CR.Message = "磁卡绑定信息错误";
                                }
                                else
                                {
                                    DataRow row = rs.Data.Tables[0].Rows[0];

                                    R.AccountNo = AccountNo;
                                    R.BillID = BillId;
                                    R.BillNo = row["FBillNo"].ToString();
                                    R.CarNumber = row["CarNumber"].ToString();
                                    R.SrcQty = Common.CDec(row["SrcQty"].ToString());
                                    R.RedQty = Common.CDec(row["RedQty"].ToString());
                                    R.RedReason = row["RedReason"].ToString();
                                    R.RedStyle = Common.CInt(row["RedStyle"].ToString());
                                    R.Item = row["ItemName"].ToString();
                                    R.Supply = row["SupplyName"].ToString();
                                    R.State = row["State"].ToString();
                                    R.UnloadStyle = row["UnloadStyle"].ToString();
                                    R.Address = row["Address"].ToString();
                                    R.Checker = row["Checker"].ToString();
                                    R.MeterType = Common.CInt(row["MeterType"].ToString());
                                    R.Note = row["Note"].ToString();
                                    R.SampNo = row["SampNo"].ToString();

                                    CR.Content = handler.SerialApi.Serializer(typeof(P01A01R), R, CR.SerialType);
                                    CR.Message = String.Format("操作员{0}获取了单据{1}的信息", user.UserName, R.BillNo);
                                }
                            }
                            else
                            {
                                CR.Result = EnumResult.Error;
                                CR.Message = "数据库错误";
                            }
                        }

                    }
                }
                else
                {
                    CR.Result = EnumResult.Error;
                    CR.Message = "数据库错误";
                }
            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = String.Format("发生错误:{0}", ex.Message);
            }
        }
    }

    public class P01A01R
    {
        public String AccountNo;
        public String BillID;
        public String BillNo;
        public Boolean NeedLogon;
        public String CarNumber;
        public String State;
        public String Item;
        public String Supply;
        public Decimal SrcQty;
        public Decimal RedQty;
        public int RedStyle;
        public String RedReason;
        /// <summary>
        /// 卸货方式
        /// </summary>
        public String UnloadStyle;
        public String Address;
        public String Checker;
        /// <summary>
        /// 过磅类型
        /// </summary>
        public int MeterType;
        /// <summary>
        /// 备注
        /// </summary>
        public String Note;
        /// <summary>
        /// 抽样编号
        /// </summary>
        public String SampNo;
    }

    public class P01A01P
    {
        /// <summary>
        /// 磁卡序列号
        /// </summary>
        public String CardSerial;
    }
}
